cmake_minimum_required(VERSION 3.0.0)
message("modbus...")
project(modbus_plugin)

#set paths
set(REPO_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR})
set(EXTERNAL_DIR ${REPO_ROOT_DIR}/deps)

#set WA_SDK_DIR paths
if(NOT DEFINED WA_SDK_DIR)
  set (WA_SDK_DIR  ${REPO_ROOT_DIR}/out/host/wa-iot)
endif()

include(${WA_SDK_DIR}/wa-sdk.cmake)
include (${WA_SDK_DIR}/rules.cmake)

add_definitions(-DLOG_MY_MASK_ID=7)

if(NOT DEFINED PLATFORM)
    set(PLATFORM x86)
endif()

# Tell the loader to look up the dependent dynamic libraries from the same folder.
set(CMAKE_INSTALL_RPATH "$ORIGIN")
set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)

set(IC_MODBUS_DIR ${CMAKE_CURRENT_LIST_DIR}/src)

set(IC_MODBUS_SOURCE
    ${IC_MODBUS_DIR}/init_modbus.c
    ${IC_MODBUS_DIR}/plugin_read_URL.c
    ${IC_MODBUS_DIR}/bus_executor.c
    ${IC_MODBUS_DIR}/plugin_write_URL.c
    ${IC_MODBUS_DIR}/bus_device.c
)

include_directories(
    ${IC_MODBUS_DIR}
)

include_directories(
    ${CMAKE_CURRENT_SOURCE_DIR}/inc
    ${CMAKE_CURRENT_SOURCE_DIR}/inc/${PLATFORM}
    ${EXTERNAL_DIR}/libmodbus/src
    ${EXTERNAL_DIR}/libmodbus
    ${EXTERNAL_DIR}
)

set(LIB_MODBUS_SOURCE
    ${EXTERNAL_DIR}/libmodbus/src/modbus.c
    ${EXTERNAL_DIR}/libmodbus/src/modbus-data.c
    ${EXTERNAL_DIR}/libmodbus/src/modbus-rtu.c
    ${EXTERNAL_DIR}/libmodbus/src/modbus-tcp.c
)

add_library(modbus SHARED ${LIB_MODBUS_SOURCE})
target_link_libraries(modbus)

set(MODBUS_SOURCE
    ${CMAKE_CURRENT_SOURCE_DIR}/src/modbus_plugin_entries.c
    ${CMAKE_CURRENT_SOURCE_DIR}/src/modbus_resource.c
    ${IC_MODBUS_SOURCE}
)

add_library(modbus_plugin MODULE ${MODBUS_SOURCE})
target_link_libraries(modbus_plugin modbus)
linkWA_SDK(modbus_plugin)

